package com.letao.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.letao.server.dto.Result;
import com.letao.server.dto.SkuInfoDTO;
import com.letao.server.dto.SpuDetailDTO;
import com.letao.server.entity.*;
import com.letao.server.mapper.OrderDetailMapper;
import com.letao.server.mapper.SkuCommentInfoMapper;
import com.letao.server.mapper.SkuInfoMapper;
import com.letao.server.mapper.SpuInfoMapper;
import com.letao.server.service.IBaseCategoryService;
import com.letao.server.service.ISkuService;
import com.letao.server.service.ISpuMediaService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SkuServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo> implements ISkuService {

    @Resource
    private SpuInfoMapper spuInfoMapper;

    @Resource
    private IBaseCategoryService baseCategoryService;

    @Resource
    private OrderDetailMapper orderDetailMapper;

    @Resource
    private SkuCommentInfoMapper skuCommentInfoMapper;

    @Resource
    private ISpuMediaService spuMediaService;

    @Override
    public Result getInfo(Long id) {
        SkuInfo sku = getById(id);
        if (sku.getBit() == 0) {
            return Result.fail("依赖不存在");
        }
        SkuInfoDTO skuInfoDTO = BeanUtil.copyProperties(sku, SkuInfoDTO.class);
        SpuInfo spuInfo = spuInfoMapper.selectById(skuInfoDTO.getSpuId());
        if (spuInfo.getBit() == 0) {
            return Result.fail("依赖不存在");
        }
        skuInfoDTO.setSpuName(spuInfo.getSpuName());
        skuInfoDTO.setSpuImage(spuInfo.getSpuImage());
        skuInfoDTO.setOneCategoryId(spuInfo.getOneCategoryId());
        skuInfoDTO.setOneCategoryName(baseCategoryService.getById(spuInfo.getOneCategoryId()).getName());
        skuInfoDTO.setTwoCategoryId(spuInfo.getTwoCategoryId());
        skuInfoDTO.setTwoCategoryName(baseCategoryService.getById(spuInfo.getTwoCategoryId()).getName());

        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sum(amount) as sales");
        queryWrapper.eq("sku_id", id);
        List<Map<String, Object>> maps = orderDetailMapper.selectMaps(queryWrapper);
        if (maps.get(0) == null) {
            skuInfoDTO.setSales(0L);
        } else {
            String sales = maps.get(0).get("sales").toString();
            skuInfoDTO.setSales(Long.valueOf(sales));
        }

        return Result.success(skuInfoDTO);
    }

    @Override
    public Result getInfoList(Long spuId) {
        SpuDetailDTO spuDetailDTO = new SpuDetailDTO();
        List<SkuInfo> skuInfos = query().eq("spu_id", spuId).eq("bit", 1).list();
        List<SkuInfoDTO> skuInfoDTOS = new ArrayList<>();

        for (SkuInfo info : skuInfos) {
            SkuInfoDTO skuInfoDTO = BeanUtil.copyProperties(info, SkuInfoDTO.class);

            QueryWrapper<SkuCommentInfo> skuCommentWrapper = new QueryWrapper<>();
            skuCommentWrapper.eq("sku_id", info.getId());
            List<SkuCommentInfo> skuCommentInfos = skuCommentInfoMapper.selectList(skuCommentWrapper);
            if (!skuCommentInfos.isEmpty()) {
                skuInfoDTO.setSkuCommentNum(skuCommentInfos.size());
            } else {
                skuInfoDTO.setSkuCommentNum(0);
            }

            QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("sum(amount) as sales");
            queryWrapper.eq("sku_id", info.getId());
            List<Map<String, Object>> maps = orderDetailMapper.selectMaps(queryWrapper);
            if (maps.get(0) == null) {
                skuInfoDTO.setSales(0L);
            } else {
                String sales = maps.get(0).get("sales").toString();
                skuInfoDTO.setSales(Long.valueOf(sales));
            }

            skuInfoDTOS.add(skuInfoDTO);
        }

        spuDetailDTO.setSkuList(skuInfoDTOS);

        spuDetailDTO.setDescript(spuInfoMapper.selectById(spuId).getDescript());
        SpuInfo spu = spuInfoMapper.selectById(spuId);
        BaseCategory one = baseCategoryService.getById(spu.getOneCategoryId());
        BaseCategory two = baseCategoryService.getById(spu.getTwoCategoryId());
        spuDetailDTO.setSpuName(spu.getSpuName());
        spuDetailDTO.setSpuImage(spu.getSpuImage());
        spuDetailDTO.setOneCategoryId(spu.getOneCategoryId());
        spuDetailDTO.setTwoCategoryId(spu.getTwoCategoryId());
        spuDetailDTO.setOneCategoryName(one.getName());
        spuDetailDTO.setTwoCategoryName(two.getName());
        spuDetailDTO.setPriceHigh(spu.getPriceHigh());
        spuDetailDTO.setPriceLow(spu.getPriceLow());

        QueryWrapper<SkuCommentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_id", spuId).select("count(*) as count");
        List<Map<String, Object>> maps1 = skuCommentInfoMapper.selectMaps(wrapper);
        spuDetailDTO.setCommentNum((Long) maps1.get(0).get("count"));

        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sum(amount) as sales");
        queryWrapper.eq("spu_id", spuId);
        List<Map<String, Object>> maps = orderDetailMapper.selectMaps(queryWrapper);
        if (maps.get(0) == null) {
            spuDetailDTO.setSales(0L);
        } else {
            String sales = maps.get(0).get("sales").toString();
            spuDetailDTO.setSales(Long.valueOf(sales));
        }
        if (maps1.get(0) == null) {
            spuDetailDTO.setCommentNum(0L);
        } else {
            String count = maps1.get(0).get("count").toString();
            spuDetailDTO.setCommentNum(Long.valueOf(count));
        }
        List<SpuMedia> list = spuMediaService.query().eq("spu_id", spuId).ne("media_type", 2).list();
        List<String> urlList = new ArrayList<>();
        if (list.isEmpty()) {
            urlList.add(spu.getSpuImage());
        }
        for (SpuMedia spuMedia : list) {
            urlList.add(spuMedia.getMediaUrl());
        }
        SpuMedia video = spuMediaService.query().eq("spu_id", spuId).eq("media_type", 2).one();
        spuDetailDTO.setSpuImageList(urlList);
        if (video != null) spuDetailDTO.setSpuVideo(video.getMediaUrl());
        return Result.success(spuDetailDTO);
    }
}
